package com.jic.order.task;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.RestResult;
import com.jic.order.api.CardOrderApi;
import com.jic.order.request.base.OrderCouponPlatformRequest;
import com.jic.order.service.OrderGoodsService;
import com.jic.order.utils.AESUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.UUID;

/**
 * 清结算调度
 */
@Component
@Slf4j
public class CardServiceTask {
    @Autowired
    private CardOrderApi cardOrderApi;
    @Autowired
    private OrderGoodsService orderGoodsService;
    /**
     * 20S查询一次
     */
    @Scheduled(fixedDelay = 1000 * 20 * 1000)
    @Async("asyncServiceExecutor")
    public void SettlementTimerTask() {
        List<JSONObject> goods = orderGoodsService.queryCardGoods();
        for(JSONObject good : goods){
            String card_order_no = good.getString("card_order_no");
            String card_id = good.getString("card_id");
            OrderCouponPlatformRequest orderCouponPlatformRequest = new OrderCouponPlatformRequest();
            orderCouponPlatformRequest.setMerchantOrderNo(card_order_no);
            orderCouponPlatformRequest.setCardId(card_id);
            orderCouponPlatformRequest.setMerchantId("WEM202011230036116");
            JSONObject cardInfo = new JSONObject();
            cardInfo.put("card_order_no",card_order_no);
            cardInfo.put("card_status",1);
            cardInfo.put("cardPassword", UUID.randomUUID().toString().substring(0,16));
            cardInfo.put("cardNo",UUID.randomUUID().toString().substring(0,16));
            cardInfo.put("id",good.getLongValue("id"));
            orderGoodsService.updateWzCardNo(cardInfo);

//            RestResult<?> ret =  cardOrderApi.getCardCouponPlatform(orderCouponPlatformRequest);
//            log.info("卡券查询:{}",ret);
//            JSONObject card = (JSONObject) ret.getData();
//            JSONObject content = card.getJSONObject("result");
//            String cardCodes = card.getString("cardCodes");
//            //00-处理中01-成功02-失败03-处理中04-已发货05-已收货06-已退货99-异常（需要查询）
//            String orderStatus = card.getString("orderStatus");
//            Integer card_status = 2;
//            switch (orderStatus){
//                case "01":card_status = 1;break;
//                case "02":card_status = 0;break;
//            }
//            //卡密
//            if(!StringUtils.isEmpty(cardCodes)){
//                String cards = AESUtils.decrp(cardCodes);
//                JSONObject cardInfo = JSONObject.parseArray(cards,JSONObject.class).get(0);
//                cardInfo.put("card_order_no",card_order_no);
//                cardInfo.put("card_status",card_status);
//                cardInfo.put("id",good.getLongValue("id"));
//                orderGoodsService.updateWzCardNo(cardInfo);
//            }else {//直冲
//                JSONObject cardInfo = new JSONObject();
//                cardInfo.put("card_order_no",card_order_no);
//                cardInfo.put("card_status",card_status);
//                cardInfo.put("id",good.getLongValue("id"));
//                orderGoodsService.updateWzCardNo(cardInfo);
//            }
        }
    }
}
